/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pcs;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import pcs.gui.RestrictionWindow;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

/**
 *
 * @author ahmet
 */
public class WorkBreakAlarmSystem {

    public void popup(String message) {
        //Here we construct a popup window
        //and show on the computer screen
        final JDialog popupWindow = new JDialog();
        popupWindow.setTitle("Posture Correction Warning");
        popupWindow.setSize(300, 100);
        popupWindow.getContentPane().setLayout(new BorderLayout());

        JLabel warningLabel = new JLabel(message, JLabel.CENTER);

        JPanel jp = new JPanel(new FlowLayout());
        JButton closeButton = new JButton("Close");
        closeButton.setMnemonic('C');
        closeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                popupWindow.dispose();
            }
        });
        jp.add(closeButton);

        popupWindow.getContentPane().add(warningLabel, BorderLayout.CENTER);
        popupWindow.getContentPane().add(jp, BorderLayout.SOUTH);

        popupWindow.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        popupWindow.setAlwaysOnTop(true);
        popupWindow.setLocationRelativeTo(null);
        popupWindow.setVisible(true);
    }

    public void sound() {
        try {
            AudioPlayer.player.start(new AudioStream(new FileInputStream("ding.wav")));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void restriction() {
        
    }

    private pcs.SystemSettings systemSettings;
    private RestrictionWindow restrictionWindow;

    public WorkBreakAlarmSystem(RestrictionWindow rw) {
        systemSettings = new SystemSettings();
        this.restrictionWindow = rw;
    }


    private Timer warnForWorkTimer;
    private Timer warnForBreakTimer;

    private Timer warnForBreakRestrictionTimer; 

    public void setWarnForBreakTimer() {
        systemSettings = null;
        systemSettings = new SystemSettings();
        double workInterval = systemSettings.getWarnForBreakTimeUntilWarning();

        if (warnForWorkTimer != null) {
            warnForWorkTimer.cancel();
            warnForWorkTimer = null;
        }

        warnForBreakTimer = new Timer("Warn for Break Timer");
        warnForBreakTimer.schedule(new WarnForBreak(), ((Double)(workInterval*60*1000)).intValue());
    }

    public void setWarnForWorkTimer() {
        systemSettings = null;
        systemSettings = new SystemSettings();
        double breakInterval = systemSettings.getWarnForWorkTimeUntilWarning();

        if (warnForBreakTimer != null) {
            warnForBreakTimer.cancel();
            warnForBreakTimer = null;
        }

        if (warnForBreakRestrictionTimer != null) {
            warnForBreakRestrictionTimer.cancel();
            warnForBreakRestrictionTimer = null;
        }

        warnForWorkTimer = new Timer("Warn for Work Timer");
        warnForWorkTimer.schedule(new WarnForWork(), ((Double)(breakInterval*60*1000)).intValue());
    }

    class WarnForBreak extends TimerTask {

        @Override
        public void run() {
            //warn the user to take a break
            if (systemSettings.isWarnForBreakWithPopup()) {
                popup("Please take a break!");
            }

            if (systemSettings.isWarnForBreakWithSound()) {
                sound();
            }

            if (systemSettings.isWarnForBreakWithRestriction()) {
                if (warnForBreakRestrictionTimer != null) {
                    warnForBreakRestrictionTimer.cancel();
                    warnForBreakRestrictionTimer = null;
                }

                warnForBreakRestrictionTimer = new Timer("Warn for Break with Restriction Timer");
                double beforeRestriction = systemSettings.getWarnForBreakTimeUntilRestriction();
                warnForBreakRestrictionTimer.schedule(new WarnForBreakRestriction(), ((Double)(beforeRestriction*1000)).intValue());
            }
        }

        class WarnForBreakRestriction extends TimerTask {
            @Override
            public void run() {
                double restrictionInterval =  systemSettings.getWarnForWorkTimeUntilWarning();
                restrictionWindow.activate("Please take a break!", true, ((Double)(restrictionInterval*60*1000)).intValue());
            }
        }
    }

    class WarnForWork extends TimerTask {
        @Override
        public void run() {
            //warn the user to go back to work
            if (systemSettings.isWarnForWorkWithPopup()) {
                popup("Please go back to work!");
            }

            if (systemSettings.isWarnForWorkWithSound()) {
                sound();
            }
        }
    }
}
